﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.IDAL;
using SD.Entities;
using System.Data.SqlClient;
using System.Data;

namespace SD.DAL
{
    public class UserDAO : BaseDAO, IUserDAO
    {

        #region IUserDAO Members

        public List<User> GetAll()
        {
            throw new NotImplementedException();
        }

        public User GetById(string userId)
        {
            SqlCommand comm = CreateCommand();
            comm.Parameters.AddWithValue("@UserID", userId);

            comm.CommandText = "GetUserByID";
            DataTable dt = ExecuteSelectCommand(comm);

            User user = null;

            // If username and password have in system
            // return a user object           
            if (dt.Rows.Count > 0)
            {
                user = new User();
                DataRow row = dt.Rows[0];
                user.UserId = row["UserID"].ToString();
                user.Account = row["Account"].ToString();
                user.Email = row["Email"].ToString();
                user.Password = row["Password"].ToString();
                user.Role = int.Parse(row["Role"].ToString());
            }
            return user;
        }

        public bool Insert(User user)
        {
            throw new NotImplementedException();
        }

        public bool Update(User user)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string userId)
        {
            throw new NotImplementedException();
        }
        
        public User Login(string username, string password)
        {
            SqlCommand comm = CreateCommand();
            comm.Parameters.AddWithValue("@Username", username);
            comm.Parameters.AddWithValue("@Password", password);

            comm.CommandText = "GetLoginUser";
            DataTable dt = ExecuteSelectCommand(comm);

            User user = null;

            // If username and password have in system
            // return a user object           
            if (dt.Rows.Count > 0)
            {
                user = new User();
                DataRow row = dt.Rows[0];
                user.UserId = row["UserID"].ToString();
                user.Account = row["Account"].ToString();
                user.Email = row["Email"].ToString();
                user.Password = row["Password"].ToString();
                user.Role = int.Parse(row["Role"].ToString());
            }
            return user;
        }
        #endregion
    }
}
